package me
import grails.converters.JSON
import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource
import groovy.sql.Sql
import groovy.json.*


class CharttemplateController {

	def dataSource

    def store() {
        def sql = new Sql(dataSource)
        def isDir = params.isDir
        def chartTemplate 
        def parentTemplateId = params.parentTemplateId
        if (isDir=="true") {
        	chartTemplate = sql.rows("select * from chart_template where is_dir=1")
        } else {
        	chartTemplate = sql.rows("""select a.*,b.theme_name,b.theme_source 
        	from chart_template a
        	left join chart_theme b on a.theme_id=b.id
        	where a.is_dir=0 and a.parent_template_id=? """,parentTemplateId)
        }


        render chartTemplate  as JSON
    }

    def save() {
    	def sql = new Sql(dataSource)
		def results = [success:true]
		def rows
	   	def isDir = params.isDir
	   	def parentTemplateId = params.parentTemplateId
	   	def templateName = params.templateName
	   	def templateSource = params.templateSource
	   	def templateDesc = params.templateDesc
	   	def templateSrc = params.templateSrc
	   	def themeId = (params.themeId==null||params.themeId=="")?null:(params.themeId as Integer)
	   	if (params.id==null || params.id=="") {
		   	if (parentTemplateId==null||parentTemplateId=="") {
		   		rows = sql.rows("select * from chart_template where template_name=? and parent_template_id is null and is_dir = ?",
	            [templateName,(isDir=="true"?1:0)])
		   	} else {
		   		rows = sql.rows("select * from chart_template where template_name=? and parent_template_id=? and is_dir=?",
	            [templateName,parentTemplateId as Integer,(isDir=="true"?1:0)])
		   	}
		   	
		   	if (rows.size()!=0) {
		   		if (isDir=="true") {
	    			results = [success:false,message:'分类已存在,请重新命名']
	    		} else {
	    			def chartTemplate = ChartTemplate.findById(rows[0].id)
	                chartTemplate.templateName = templateName    
	                chartTemplate.templateSrc = templateSrc
	                chartTemplate.templateDesc = templateDesc
	                chartTemplate.isDir = false
	                println "ddd"+themeId+"eeee"
	                chartTemplate.themeId = themeId
	                chartTemplate.parentTemplateId = (parentTemplateId==null||parentTemplateId=="")?null:(parentTemplateId as Integer)  
	                chartTemplate.templateSource = templateSource  
	                try {
	                    chartTemplate.save(failOnError: true,flush: true) 
	                } catch (e) {
	                    println e
	                }
	                results.chartTemplate = chartTemplate
	    		}
	    		
	    	} else {
	    		if (isDir=="true") {
	    			def chartTemplate = new ChartTemplate(templateName:templateName,isDir:true)
	                try {
	                    chartTemplate.save(failOnError: true,flush: true) 
	                } catch (e) {
	                    println e
	                }
	                results.chartTemplate = chartTemplate
	    		} else {
	    			def chartTemplate = new ChartTemplate(templateName:templateName,templateSrc:templateSrc,
	    			templateSource:(templateSource==null||templateSource=="")?"{}":templateSource,
	    			templateDesc:templateDesc,isDir:false,parentTemplateId:parentTemplateId as Integer,themeId:themeId)
	                try {
	                    chartTemplate.save(failOnError:true,flush:true) 
	                } catch (e) {
	                    println e
	                }
	                results.chartTemplate = chartTemplate     
	    		}		
	    	}
	    } else {
	    	def chartTemplate = ChartTemplate.findById(params.id)
	        chartTemplate.templateName = templateName    
	        try {
	            chartTemplate.save(failOnError: true,flush: true) 
	        } catch (e) {
	            println e
	        }
	        results.chartTemplate = chartTemplate
	    }
    	render results  as JSON

    }

    def delete() {
    	def sql = new Sql(dataSource)
		def results = [success:true]

		sql.execute("delete from chart_template where id = ?",params.id as Integer)
		sql.execute("delete from chart_template where parent_template_id=?",params.id as Integer)



		render results  as JSON
    }
   	
}
